import {changeSystemLanguage} from './util/util'
App({
  onLaunch() {
    //check for new version
    this.checkNewVersion();
    //config system info
    this.initSystemInfo();
    //check for language
    this.checkLanguage();
  },
  globalData: {
    environment: wx.getAccountInfoSync().miniProgram.envVersion, //get environment
    loginStatus:wx.getStorageSync('loginStatus')?wx.getStorageSync('loginStatus'):false,
    systemConfig: { //手机系统配置
      theme: 'light',
      language: 'zh_CN',
      languageObj:''
    },
    //菜单
    Memu: [{
        name: '定位导航',
        languageKey: 'gps',
        isLock: false,
        src: './image/location.png',
        color: '#409EFF',
        class: 'audit',
        url: '/Location/pages/index/index'
      },
      {
        name: '蓝牙连接',
        languageKey: 'blue',
        isLock: true,
        src: './image/blueTooth.png',
        color: '#67C23A',
        class: 'inspaction',
        url: '/BlueTooth/pages/index/index'
      },
      {
        name: '图像识别',
        languageKey: 'ai',
        isLock: false,
        src: './image/AI.png',
        color: '#F56C6C',
        class: 'installed',
        url: '/AI/pages/index/index'
      },
      {
        name: '敬请期待',
        languageKey: 'stayTuned',
        isLock: true,
        src: './image/stayTuned.png',
        color: '#F5B350',
        class: 'operation',
        url: '/About/pages/index/index'
      }
    ]
  },
  //check for new version
  checkNewVersion() {
    const updateManager = wx.getUpdateManager();
    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
      if (res.hasUpdate) {
        wx.showLoading({
          title: "新版本下载..."
        });
      }
    })
    updateManager.onUpdateReady(function () {
      wx.hideLoading();
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好，请重启应用',
        success: function (res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      });
    })
    updateManager.onUpdateFailed(function () {
      wx.showToast({
        title: '下载失败，请重新进入',
        icon: 'none',
      })
    })
  },
  //config system info
  initSystemInfo() {
    const systemInfo = wx.getSystemInfoSync();
    this.globalData.systemConfig.theme = systemInfo.theme;
    this.globalData.systemConfig.language = systemInfo.language;
  },
  //check language
  checkLanguage() {
    // 判断系统语言 确定小程序语言
    changeSystemLanguage(this.globalData.systemConfig.language) ;
  }
})